package com.doge.controller.admin;

import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.base.BaseRespVo;
import com.doge.bean.bo.goods.CreateGoodsBO;
import com.doge.bean.bo.goods.UpdateGoodsBO;
import com.doge.bean.pojo.goods.Goods;
import com.doge.bean.vo.goods.CatAndBrandVO;
import com.doge.bean.vo.goods.GoodsDetailVO;
import com.doge.exception.Delete502Exception;
import com.doge.service.admin.goods.GoodsService;
import com.doge.util.anno.MarketHibernateValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @program: project2-wechat-mall
 * @description:
 * @author: 305905917@qq.com
 * @create: 2021-11-29 21:33
 **/
@RestController
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    /**
     * http://182.92.235.201:8083/admin/goods/list?page=1&limit=20&sort=add_time&order=desc
     * 通过请求参数获取商品数据，然后用json返回
     *http://182.92.235.201:8083/admin/goods/list?
     * page=1&limit=20&goodsSn=&name=%E5%A4%A9%E7%84%B6&sort=add_time&order=desc&goodsId=
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(BaseParam baseParam,String goodsSn,String name,Integer goodsId) {
        BaseData goodsList = goodsService.list(baseParam, goodsSn, name, goodsId);
        return BaseRespVo.ok(goodsList);
    }

    /**
     * 添加商品
     * @param createGoodsBO
     * @return
     */

    @MarketHibernateValid
    @PostMapping("create")
    public BaseRespVo create(@Valid @RequestBody  CreateGoodsBO createGoodsBO,BindingResult bindingResult) {
        goodsService.crateGoods(createGoodsBO);
        return BaseRespVo.ok();
    }

    /**
     * 修改商品详情
     * @param updateGoodsBO
     * @return
     */
    @MarketHibernateValid
    @PostMapping("update")
    public BaseRespVo update(@Valid @RequestBody UpdateGoodsBO updateGoodsBO ,BindingResult bindingResult) {
        goodsService.update(updateGoodsBO);
        return BaseRespVo.ok();
    }

    /**
     * 逻辑删除商品 delete变为true则为删除
     * @param goods
     * @return
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Goods goods) throws Delete502Exception {
        goodsService.deleteGoods(goods);
        return BaseRespVo.ok();
    }

    /**
     * 编辑商品回显
     * @param id
     * @return
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        GoodsDetailVO goodsDetailVO = goodsService.selectDetailByGoodsId(id);
        return BaseRespVo.ok(goodsDetailVO);
    }


    /**
     * 显示商品上架的所属分类和所属品牌商
     * @return
     */
    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand() {
        CatAndBrandVO catAndBrandVO = goodsService.catAndBrand();
        return BaseRespVo.ok(catAndBrandVO);
    }

}
